package com.xiaoqi.store.service.impl;

import com.xiaoqi.store.dao.AddressMapper;
import com.xiaoqi.store.dao.DistrictMapper;
import com.xiaoqi.store.entity.AddressEntity;
import com.xiaoqi.store.exception.impl.*;
import com.xiaoqi.store.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description 用户地址业务实现类
 * @Date
 **/
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private DistrictMapper districtMapper;

    /*
    * @Author xiaoqi
    * @Description  添加用户收货地址
    * @return
    **/
    @Override
    public void addUserAddress(AddressEntity addressEntity, Integer uid, String username) {
        // 1.判断当前用户是否被删除（作为安全性考虑，但是为了节省效率，不查了）
        // 如果被删除，抛出异常

        // 获取省市级名称
        String provinceCode = addressEntity.getProvinceCode();
        String cityCode = addressEntity.getCityCode();
        String areaCode = addressEntity.getAreaCode();
        String provinceName = districtMapper.selectDistrictsName(provinceCode);
        String cityName = districtMapper.selectDistrictsName(cityCode);
        String areaName = districtMapper.selectDistrictsName(areaCode);

        // 获取当前用户的地址数量
        int i = addressMapper.selectUserAddressCount(uid);
        log.info("获取当前用户地址数量"+ i);

        // 2.判断当前用户的收货地址数量是否大于20
        // 如果大于，抛出异常
        if (i>=20){
            log.info("收货数量超出限制");
            throw new AddressCountOutOfBounds("收货数量超出限制");
        }
        // 补全日志数据
        Date date = new Date();
        addressEntity.setCreatedTime(date);
        addressEntity.setCreatedUser(username);
        addressEntity.setUid(uid);
        addressEntity.setProvinceName(provinceName);
        addressEntity.setCityName(cityName);
        addressEntity.setAreaName(areaName);

        // 3.添加用户地址
        // 判断当前用户是否有收货地址
        // 如果没有，添加的这个作为默认地址
        if (i==0){
            addressEntity.setIsDefault(1);
            int addUserAddressResult = addressMapper.addUserAddress(addressEntity);
            // 添加失败，抛出异常
            if (addUserAddressResult<=0){
                log.info("添加用户地址出现系统错误");
                throw new AddressAddException("添加用户地址出现系统错误");
            }
            else {
                log.info("添加用户地址成功");
            }
        }
        // 如果有，将这个地址添加到后边，并且设置默认地址为0
        if (i!=0 && i>0){
            addressEntity.setIsDefault(0);
            int addUserAddress = addressMapper.addUserAddress(addressEntity);
            // 添加失败 ，抛出添加异常
            if (addUserAddress <=0){
                log.info("添加用户地址出现系统错误");
                throw new AddressAddException("添加用户地址出现系统错误");
            }
            else {
                log.info("添加用户地址成功");
            }
        }


    }

    /*
    * @Author xiaoqi
    * @Description  查询用户的收货地址
    * @return
    **/
    @Override
    public List<AddressEntity> selectUserAllAddress(Integer uid) {

        List<AddressEntity> userAddressEntityList =  addressMapper.selectUserAllAddress(uid);
        // 这里不需要判断收获地址是不是null
        return userAddressEntityList;
    }

    /*
    * @Author xiaoqi
    * @Description  修改用户默认收货地址
    * @return
    **/
    @Transactional
    @Override
    public void updateUserDefaultAddress(Integer aid,Integer uid,String username) {

        // 判断收货地址是否为null
        AddressEntity userAddressEntityByAid = addressMapper.findUserAddressByAid(aid);
        if (userAddressEntityByAid == null){
            log.info("收货地址为空，请先添加收货地址");
            throw new AddressUpdateException("请先添加收货地址");
        }
        // 判断当前修改用户的uid与session中存在的uid是否相同
        if (userAddressEntityByAid.getUid() != uid){
            log.info("非法用户，请滚蛋");
            throw new AddressUpdateIllegality("非法用户，请滚蛋");
        }

        // 添加日志信息
        Date date = new Date();

        // 将当前用户的所有的收货地址先设置为不是默认的收货地址
        int setNoDefaultResult = addressMapper.updateUserDefaultAddressToNone(uid);
        // 判断更新是否成功
        if (setNoDefaultResult<=0){
            // 抛出异常
        }
        // 将传入的aid编号的收货地址设置为默认的收货地址
        int setDefaultResult = addressMapper.updateUserDefaultAddress(aid);
        // 判断更新是否成功
        if (setDefaultResult<=0){
            // 抛出异常
            log.info("用户收货地址更新失败");
            throw new AddressUpdateException("用户收货地址更新失败");
        }

    }

    /*
    * @Author xiaoqi
    * @Description  根据指定aid删除收货地址
    * @return
    **/
    @Override
    public void deleteAddressByAid(Integer aid,Integer uid) {
        // 查询该编号的收货地址是否存在
        AddressEntity userAddressEntityByAid = addressMapper.findUserAddressByAid(aid);
        // 判断删除的用户编号与session中存在的用户是否相同
        if (userAddressEntityByAid.getUid() != uid){
            log.info("非法用户，禁止删除");
            throw new AddressUpdateIllegality("非法用户，禁止删除");
        }
        // 执行删除方法
        int deleteAddressResult = addressMapper.deleteAddressByAid(aid);
        // 判断删除是否成功
        if (deleteAddressResult<=0){
            log.info("用户收货地址删除出错");
            throw new AddressDeleteException("收货地址删除出错");
        }
        log.info("删除收货地址成功");
    }
}
